const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      rules: [],
      options: [],
      selectedProvince: '',
      provinceForInput: '',
      form: {
        Province_Name: '',
        ProvinceNumber: '',
        Province: '',
        Picture_URL: '',
        Details_URL: '',
      },
      pic:'',
      pic2:'',
      loading: false
    }
  },
  mounted() {
    this.getPrince()
    this.uploadFront()
    this.uploadFront2()
  },
  methods: {
    getPrince() {
      console.log('执行了')
      const _this = this
      $.ajax({
        url: DomainName + `/getInquiryAction?token=${$.cookie('token')}`,
        type: 'POST',
        data: {
          Data: 'system_province',
          isNullEnterprise: true
        },
        success: function (res) {
          // console.log('----------',res);
          _this.options = res
          console.log('数据', _this.options)
        }
      })
    },

    //商品封面图片
    uploadFront(file) {
      console.log('上传第一张图片')
      let _this = this
      let num = 1
      // if (file.size > 150000 && file.size < 500000) {
      //   num = 0.4
      // } else if (file.size > 500000) {
      //   num = 0.2
      // }
      new Compressor(file, {
        quality: num,
        // if(file.size) {
        // }
        success(file) {
          console.log(file.size)
          const formData = new FormData();
          formData.append('file', file, file.name);
          let oFileReader = new FileReader();
          oFileReader.readAsDataURL(file);
          oFileReader.onload = function (e) {
            $.ajax({
              type: 'POST',
              url: DomainName + '/FileList',
              data: {
                Data: e.target.result,
                DataLength: file.size,
                FileName: file.name
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              },
              success: function (Result) {
                _this.pic = e.target.result
                if (Result.Status == "success") {
                  _this.form.Picture_URL = Result.Picture_Url
                  _this.$message.success('上传成功')
                  console.log(Result.Picture_Url);
                } else {
                  _this.$message.error('上传失败')
                }
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              dataType: "json"
            });
          }
        },
        error(err) {
          console.log(err.message);
        },
      });
    },
    //商品封面图片
    uploadFront2(file2) { 
      console.log('上传第二张图片')
      let _this = this
      let num = 1
      new Compressor(file2, {
        quality: num,
        success(file2) {
          console.log(file2.size)
          const formData = new FormData();
          formData.append('file', file2, file2.name);
          let oFileReader = new FileReader();
          oFileReader.readAsDataURL(file2);
          oFileReader.onload = function (e) {
            $.ajax({
              type: 'POST',
              url: DomainName + '/FileList',
              data: {
                Data: e.target.result,
                DataLength: file2.size,
                FileName: file2.name
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              },
              success: function (Result) {
                _this.pic2 = e.target.result
                if (Result.Status == "success") {
                  _this.form.Details_URL = Result.Picture_Url
                  _this.$message.success('上传成功')
                  console.log(Result.Picture_Url);
                } else {
                  _this.$message.error('上传失败')
                }
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              dataType: "json"
            });
          }
        },
        error(err) {
          console.log(err.message);
        },
      });
    },

    submitForm(formName) {
      let _this = this
      console.log({
        article_province: [_this.form]
      });
      $.ajax({
        url: DomainName + `/getAddAction?token=${$.cookie('token')}`,
        type: 'POST',
        data: {
          Data: JSON.stringify({
            article_province: [_this.form]
          }),
        },
        success: function (res) {
          console.log('提交结果', res)
          _this.$message.success("提交成功")
          window.history.go(-1);
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      });
    },
    handleSelectProvince(selectedProvince) {
      // this.provinceForInput = selectedProvince.slice(0, -1);
      this.options.forEach(item=>{
        if(item.ProvinceNumber == selectedProvince){
          this.form.Province_Name = item.Province
          this.form.Province = item.Province
          this.form.ProvinceNumber = item.ProvinceNumber
        }
      })
      console.log(selectedProvince);
    },
  }
})